package com.taojiazheng.serviceImpl.user;

import com.taojiazheng.common.ApplicationException;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.entity.User;
import com.taojiazheng.entity.Userinfo;
import com.taojiazheng.mapper.UserinfoMapper;
import com.taojiazheng.service.user.UserService;
import com.taojiazheng.so.user.UserSo;
import com.taojiazheng.so.user.UserinfoSo;
import com.taojiazheng.vo.user.UserInfoVo;
import com.taojiazheng.vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 赵成林
 * @date 2017/11/22 20:20
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserinfoMapper userinfoMapper;
    public User getUserByToken(String token) {
        User user = userDao.getUserByToken(token);
        return user;
    }

    public int insertUser(UserSo userSo) {
        int getId = userDao.insertUser(userSo);
        return userSo.getId();
    }

    public String createToken(String phone) {
        StringBuilder stringBuilder = new StringBuilder(new Date().getTime() + "");
        stringBuilder.append(phone);
        String token = stringBuilder.toString();
        return token;
    }

    public User getUserByPhone(String phone) {
        return userDao.getUserByPhone(phone);
    }

    public User getUserByName(String name) {
        // return null;
        return userDao.getUserByName(name);
    }

    @Transactional
    public UserVo register(UserSo userSo) {
        if (userSo != null && (userSo.getPhone() == null)) {
            throw new ApplicationException(203, "用户信息不完整");
        }
        User getUserByName = this.getUserByName(userSo.getUserName());
        if (getUserByName != null) {
            throw new ApplicationException(203, "用户名被占用");
        }
        User getUserByPhone = this.getUserByPhone(userSo.getPhone());
        if (getUserByPhone != null) {
            throw new ApplicationException(203, "手机号已经注册");
        }
        String token = this.createToken(userSo.getPhone());
        userSo.setToken(token);
        int userId = this.insertUser(userSo);
        UserVo userVo = new UserVo();
        userVo.setId(userId);
        userVo.setToken(token);
        userVo.setUserName(userSo.getUserName());
        userVo.setPhone(userSo.getPhone());
        return userVo;
    }

    @Transactional
    public UserVo login(UserSo userSo) {
        User getUserByName = this.getUserByName(userSo.getUserName());
        if (getUserByName == null) {
            throw new ApplicationException(204, "用户不存在");
        }
        if (!getUserByName.getPassword().equals(userSo.getPassword())) {
            throw new ApplicationException(204, "密码错误");
        }
        String token = this.createToken(userSo.getPhone() == null ? userSo.getUserName() : userSo.getPhone());
        userSo.setToken(token);
        userSo.setId(getUserByName.getId());
        this.updateToken(userSo);
        UserVo userVo = new UserVo();
        userVo.setUserName(userSo.getUserName());
        userVo.setToken(token);
        userVo.setId(getUserByName.getId());
        return userVo;
    }

    public int updateToken(UserSo userSo) {
        return userDao.updateToken(userSo);
    }

    /**
     * 完善用户信息
     * 返回用户id
     */
    public int createUserInfo(UserinfoSo userinfoSo, String token){
        Userinfo userinfo = new Userinfo();
        User user = userDao.getUserByToken(token);
        int userId = user.getId();
        userinfo.setUserId(userId);
        userinfo.setAge(userinfoSo.getAge());
        userinfo.setIdnumber(userinfoSo.getIdnumber());
        System.out.println(userinfoSo.getIdnumber());
        userinfo.setRealName(userinfoSo.getRealName());
        userinfo.setSex(userinfoSo.getSex());
        userDao.createUserInfo(userinfo);
        return userId;
    }

    @Override
    public Boolean isCreateUserInfo(String token) {
        //return null;
        User user = userDao.getUserByToken(token);
        Integer userId = user.getId();
        Userinfo userinfo = userinfoMapper.getUserByUserId(userId);
        if(userinfo != null){
            return true;
        }
        return false;
    }

    @Override
    public void deleteUserInfo(String token) {
        User user = userDao.getUserByToken(token);
        userinfoMapper.deleteUserInfo(user.getId());
    }
}
